package com.service.impl;

import com.bean.entity.StudentInfo;
import com.bean.pojo.PageAssistant;
import com.bean.vo.StudentInfoVo;
import com.dao.StudentInfoMapper;
import com.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class StudentInfoServiceImpl implements StudentInfoService {

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    @Override
    public StudentInfoVo studentLogin(StudentInfo studentInfo) throws Exception {
        List<StudentInfoVo> studentInfoVoList = studentInfoMapper.select(studentInfo);
        System.out.println(studentInfoVoList);

        if (studentInfoVoList.size()!=1) throw new Exception("登录失败，请输入正确的账号密码！");
        System.out.println(studentInfoVoList.get(0));
        return studentInfoVoList.get(0);
    }

    @Override
    public int save(StudentInfo t) throws Exception {
        int insert = studentInfoMapper.insert(t);
        if (insert==1) return 1;
        throw new Exception("学生信息添加失败，请正确输入！");
    }

    @Override
    public int remove(StudentInfo t) throws Exception {
        int delete = studentInfoMapper.delete(t);
        if (delete==1) return 1;
        throw new Exception("学生信息删除失败！");
    }

    @Override
    public int modify(StudentInfo t) throws Exception {
        int update = studentInfoMapper.update(t);
        if (update==1) return 1;
        throw new Exception("学生信息修改失败，请稍后重试！");
    }

    @Override
    public StudentInfoVo unique(StudentInfo t) throws Exception {
        List<StudentInfoVo> select = studentInfoMapper.select(t);
        if (select.size()!=1) throw new Exception("您要查询的学生信息不存在，请重新操作！");
        return select.get(0);
    }

    @Override
    public List<StudentInfoVo> query(StudentInfo t) {
        return studentInfoMapper.select(t);
    }

    @Override
    public PageAssistant<StudentInfoVo> queryByPage(Map<String, Object> map, PageAssistant<StudentInfoVo> pa) {
       //查询并设置总记录数
        pa.setCount(studentInfoMapper.selectCount(map));

        //添加分页参数
        map.put("startCode",pa.getStartCode());
        map.put("pageSize",pa.getPageSize());

        //查询并设置页面内容
        pa.setItems(studentInfoMapper.selectByPage(map));
        return pa;
    }
}
